import ChartBase from "./chart-base";
import {
  grid,
  legend,
  gridConfig,
  legendConfig,
  pieSeriesLabel,
  pieSeriesLabelLine,
  pieSeriesLabelConfig,
  pieSeriesLabelLineConfig,
  tooltip,
  tooltipConfig,
} from "./common";
import { deepClone, mergeObject } from "@/utils";

export default class SunburstCustom extends ChartBase {
  constructor() {
    super();
  }

  // 基础描述
  _info = {
    name: "定制化业务系统分布图",
    type: "sunburst-custom", // 图表类型

    group: "sunburst", // 图表分类
    component: "sunburst-custom",
    icon: "sunburst-custom", // 图表展示ICON
  };

  // 总 配置项
  _defaultOption = {
    dataset: {
      source: [],
      dimensions: [],
    },
    grid,
    legend: {
      show: true,
      top: "5%",
    },
    tooltip: {
      show: true,
    },
    series: [],
  };
  // 系列
  _defaultSeries = {
    radius: ["15%", "80%"],
    center: ["50%", "55%"],
    type: "sunburst",
    sort: undefined,
    nodeClick: false,
    emphasis: {
      focus: "ancestor",
    },
    data: [],
    label: {
      show: true,
      color: "#FFFFFF",
      rotate: "radial",
      textBorderWidth: 2,
      formatter: function (param) {
        var depth = param.treePathInfo.length;
        return depth === 2 ? param.data.systemName : " ";
      },
    },
    tooltip: {
      formatter: function (param) {
        return `<div>${param.data.systemName}</div>
            <div>${param.name}：  ${param.value}</div>`;
      },
    },
    levels: [
      {},
      {
        itemStyle: {
          color: "#CD4949",
        },
        label: {
          rotate: "tangential",
        },
      },
      {
        itemStyle: {
          color: "#F47251",
        },
        label: {
          rotate: "radial",
        },
      },
      {
        itemStyle: {
          color: "#FFC75F",
        },
        label: {
          rotate: "radial",
        },
      },
    ],
  };

  // 数据结构描述
  _dataPoolConfig = [
    {
      label: "字段",
      key: "field",
      type: ["string", "date"],
      limit: -1,
    },
    {
      label: "数据",
      key: "value",
      type: ["number"],
      limit: -1,
    },
  ];

  // 样式配置描述信息
  _styleOptionConfig = [
    {
      label: "文字配置",
      bindKeys: "_group.legend",
      controlType: "group",
      controlValue: "",
      controlProps: {},
      controlConfigs: {},
    },
    {
      label: "显示",
      bindKeys: "config.label.show",
      controlType: "switch",
      controlValue: true,
      controlProps: {},
      controlConfigs: {},
    },
  ];

  getOption({
    dataField = [],
    dataList = [],
    dataTotal = 0,
    dataPoolField,
    styleOptionConfig = [],
    themeColor = [],
    themeStyle = {},
  }) {
    // 更新描述字段
    this._updateDataPoolField(dataField, dataPoolField);

    const { field, value } = dataPoolField;

    let { option, series, config } =
      this._transformStyleOptionConfig(styleOptionConfig);

    let defaultOption = deepClone(this._defaultOption);
    let defaultSeries = deepClone(this._defaultSeries);

    // 再合并自定义配置项
    if (defaultOption && option)
      defaultOption = mergeObject(defaultOption, option);
    if (defaultSeries && series)
      defaultSeries = mergeObject(defaultSeries, series);

    defaultOption.color = themeColor;

    defaultSeries.label.show = config.label.show;

    let systemField = field[0]?.fieldName,
      tempData = [],
      coreField = value.find((f) => f.fieldDescribe === "核心数据")?.fieldName,
      importField = value.find(
        (f) => f.fieldDescribe === "重要数据"
      )?.fieldName,
      senField = value.find((f) => f.fieldDescribe === "敏感数据")?.fieldName;

    if (systemField && coreField && importField && senField) {
      tempData = dataList.reduce((r, c) => {
        r.push({
          [systemField]: c[systemField],
          systemName: c[systemField],
          name: "敏感数据",
          value: c[senField],

          children: [
            {
              systemName: c[systemField],
              name: "重要数据",
              value: c[importField],
              systemName: c[systemField],

              children: [
                {
                  systemName: c[systemField],
                  name: "核心数据",
                  value: c[coreField],
                },
              ],
            },
          ],
        });
        return r;
      }, []);
    }
    if (
      this._info.component === "sunburst-custom" &&
      defaultOption.series &&
      defaultOption.series instanceof Array
    ) {
      defaultOption.series.push({
        ...defaultSeries,
        data: tempData,
      });
    }

    return defaultOption;
  }

  getClickData(event) {
    const { name, seriesName, value, data } = event;

    return {
      label: [
        data.name === "重要数据"
          ? "import"
          : data.name === "核心数据"
          ? "core"
          : "type",
        data.systemName,
      ],
      value: [
        data.name === "重要数据"
          ? "import"
          : data.name === "核心数据"
          ? "core"
          : "type",
        data.systemName,
      ],
    };
  }
}
